package GUI;


import javax.swing.*;

import client.ClientConsole;

import Library.ListOrderBookStudent;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;


/**
 * loan book gui
 * @author Maor
 *
 */
public class GUI_loan extends JPanel {
	/** text field for reader id*/
	private JTextField textField;
	/**
	 * panel that we back to him when we press 'back'
	 */
	private ClientConsole clc;
	/**
	 * number of copies
	 */
	private int countCopies;
	/**
	 * for chose the copy that we want to loan
	 */
	private JComboBox copiesBox;
	/**
	 * for chose the book that we want to loan
	 */
	private JComboBox booksBox;
	/**
	 * current choise of book
	 */
	private ItemEvent curBook;
	private GUI_LogIn gl;
	/** panel that we back to him*/
	private JPanel backPanel; 
	private GUI_loan guiLoan;
	
	/**
	 * constructor that called from librerian menu, when the librerian want to make new loan
	 * @param cc  clientConsole
	 * @param gl  the main frame window
	 * @param lm  libreian menu, for back
	 * @wbp.parser.constructor
	 */
	public GUI_loan(ClientConsole cc, final GUI_LogIn gl,final GUI_LibrarianMenu lm) {
		setLayout(null);
		this.setSize(700, 600);
		clc=cc;
		this.gl=gl;
		backPanel=lm;
		same();
	}

	/**
	 * 
	* constructor that called from Manager menu, when the Manager want to make new loan
	 * @param cc  clientConsole
	 * @param gl  the main frame window
	 * @param mm  Manager menu, for back
	 */
	public GUI_loan(ClientConsole cc, final GUI_LogIn gl,final GUI_ManagerMenu mm) {
		setLayout(null);
		this.setSize(700, 600);
		clc=cc;
		this.gl=gl;
		backPanel=mm;
		same();
	}
	
	/**
	 * same func for liberian and manager for make the choise
	 * fo book and copy that we want to make loan for them
	 */
	public void same()
	{
		guiLoan=this;
		JLabel lblLoanBook = new JLabel("Loan Book");
		lblLoanBook.setBounds(182, 11, 78, 14);
		add(lblLoanBook);
		
		JLabel lblReaderId = new JLabel("Reader id");
		lblReaderId.setBounds(44, 72, 63, 14);
		add(lblReaderId);
		
		textField = new JTextField();
		textField.setBounds(129, 69, 86, 20);
		add(textField);
		textField.setColumns(10);
		

		
		JButton btnSubmit_1 = new JButton("Submit");
		btnSubmit_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String reader=textField.getText();
				
				if(reader.equals("")||countCopies==0)
				{
					JOptionPane.showMessageDialog(new JFrame(),"There is an empty field");
				}
				else{
				String copy=(String) copiesBox.getSelectedItem();
				String[] s={"new loan",reader,copy,"regular"};
				try {
					clc.client.sendToServer(s);
					while(clc.client.flag==0)
						System.out.print("");
					clc.client.flag=0;
					JOptionPane.showMessageDialog(new JFrame(),clc.client.message);
					textField.setText(null);
					if(clc.client.message.equals("book loaned for 3 days")||clc.client.message.equals("book loaned for 14 days"))
						setCopiesItem();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			}
		});
		
		btnSubmit_1.setBounds(79, 201, 89, 23);
		add(btnSubmit_1);
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.setContentPane(backPanel);
			}
		});
		btnBack.setBounds(206, 201, 89, 23);
		add(btnBack);
		
		
		
		
		JLabel lblCopyAviable = new JLabel("copies aviable");
		lblCopyAviable.setBounds(249, 118, 86, 14);
		add(lblCopyAviable);
		
		 copiesBox = new JComboBox();
		copiesBox.setBounds(345, 115, 78, 20);
		add(copiesBox);
	
				
				
		JLabel lblBookId = new JLabel("Book id");
		lblBookId.setBounds(49, 118, 46, 14);
		add(lblBookId);	
		
		
		 booksBox = new JComboBox();
		 booksBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				curBook=e;
				setCopiesItem();
			}
		}
		);
		try {
			String[] s={"getBooks"};
			clc.client.sendToServer(s);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while(clc.client.flag==0)
			System.out.print("");
		clc.client.flag=0;
		ArrayList<String[]> result;
		result=clc.client.details;
		Iterator i=result.iterator();
		while(i.hasNext())
		{
		String[] res=(String[]) i.next();
		booksBox.addItem(res[0]);
		}
		booksBox.setBounds(129, 115, 86, 20);
		add(booksBox);
		
		JButton btnLogOut = new JButton("log out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.LogOut();
			}
		});
		btnLogOut.setBounds(352, 201, 89, 23);
		add(btnLogOut);
		
		JButton btnWaitingBooks = new JButton("waiting books");
		btnWaitingBooks.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String readerId=textField.getText();
				 String[] s={"getuserName",readerId};
				 if(readerId.equals(""))
					{
						JOptionPane.showMessageDialog(new JFrame(),"There is an empty field");
					}
				 else{
				 try {
					clc.client.sendToServer(s);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				while(clc.client.flag==0)
				     System.out.print("");
			    clc.client.flag=0;
			    String userName=(String) clc.client.message;
				String s2[]={"getListOrderBook",userName};
				try {
					clc.client.sendToServer(s2);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
						while(clc.client.flag==0)
						    System.out.print("");
			    clc.client.flag=0;
				ListOrderBookStudent listOrderBookStudent =(ListOrderBookStudent)clc.client.message;
				if(listOrderBookStudent.ListOrderBookStudent.isEmpty())
				    {
				    JOptionPane.showMessageDialog(new JFrame(),"no books waiting");
				    }
				    else
				    {
				    	GUI_ShowOrderBook sob= new GUI_ShowOrderBook(clc,listOrderBookStudent.ListOrderBookStudent,guiLoan,readerId);
				    	clc.cgui.setContentPane(sob);
				    }
				    	
				 }
			}
		});
		btnWaitingBooks.setBounds(246, 68, 117, 23);
		add(btnWaitingBooks);
	}
	
	/**
	 * refresh the copy box
	 */
	private void setCopiesItem()
	{
		copiesBox.removeAllItems();
		try {
			String[] s={"getCopies"};
			clc.client.sendToServer(s);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while(clc.client.flag==0)
			System.out.print("");
		clc.client.flag=0;
		ArrayList<String[]> result;
		result=clc.client.details;
		Iterator i=result.iterator();
		countCopies=0;
		while(i.hasNext())
		{
		String[] res=(String[]) i.next();
		if((res[1].equals(curBook.getItem()))&&(res[2].equals("1")))
		{
			copiesBox.addItem(res[0]);
			countCopies++;
		}
		
		
		
		
	}
		
	}	
}


